﻿#coding=utf-8
import os,time
from down.models import *
from wap.models import FriendLink
from django.core.exceptions import ObjectDoesNotExist
from django.http import HttpResponse
from django.http import HttpRequest
from django.http import HttpResponseRedirect

from django.template import loader
from django.shortcuts import render_to_response
from django.db.models import Q


from django.views.generic import list_detail
from django.shortcuts import get_object_or_404
from django.core.mail import send_mail, BadHeaderError

from django.core.cache import cache  


#显示WAP,成功
def show_wap_index(request):
    #今日软件推荐
    soft_list = Soft.news.all()[:5]
    #今日游戏推荐
    
    if cache.get('game_list','No Cache')!='No Cache' and cache.get('game_count','No Cache')!='No Cache':
       game_list = cache.get('game_list')
       game_count = cache.get('game_count')
       print 'has cache'
       print game_list
    else:
       game_list = Game.objects.order_by('?')
       game_count = game_list.count()
    
       game_list = game_list[0:5]
       for game in game_list:
          game.name = game.name
       cache.set('game_list', game_list)
       cache.set('game_count',game_count)
       print 'none cache'
    #今日小说推荐
    if cache.get('book_list','No Cache')!='No Cache' and cache.get('book_count','No Cache')!='No Cache':
       book_list = cache.get('book_list')
       book_count = cache.get('book_count')
    else:
       book_list = Book.objects.order_by('?')
       book_count = book_list.count()
    
       book_list = book_list[0:5]
       for book in book_list:
          book.name = book.name
       cache.set('book_list', book_list)
       cache.set('book_count',book_count)
    
    #软件总数
    if cache.get('soft_count','No Cache')!='No Cache':
       soft_count = cache.get('soft_count')
    else:
       soft_count = Soft.objects.all().count()
       cache.set('soft_count', soft_count)
       
    
    
   
    #游戏机型
    if cache.get('phone_list','No Cache')!='No Cache':
       phone_list = cache.get('phone_list')
    else:
       phone_list = Phone.objects.order_by('-count')[0:10]
       for phone in phone_list:
           phone.phone_name = phone.phone_name
       cache.set('phone_list', phone_list)

    
    phone_list1 = phone_list[0:5]
    phone_list2 = phone_list[5:10]


    t = time.localtime()
    nowtime = time.strftime("%H:%M:%S",t) 
    
    return HttpResponse(loader.render_to_string('wap/index.wml',{'time':nowtime,'phone_list1':phone_list1,'phone_list2':phone_list2,'soft_count':soft_count,'game_count':game_count,'book_count':book_count,'soft_list':soft_list,'game_list':game_list,'book_list':book_list}),"text/vnd.wap.wml")

def show_wap_bookmark(request):
    return HttpResponse(loader.render_to_string('wap/bookmark.wmlc',{}),"text/vnd.wap.wml")
    

#Wap快车道
def search_by_wapid(request):
    q_wapid = request.GET.get('wapid', '')
    
    if q_wapid == '':
        return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    q_wapid = int(q_wapid)
    
    object_list = Soft.objects.filter(wapid=q_wapid).distinct()
   
    if object_list :
        model_name = '软件'
	object = object_list[0]
        return HttpResponse(loader.render_to_string('wap/search_detail.wml',{'model_name':model_name,'object':object}),"text/vnd.wap.wml")
    
    object_list = Game.objects.filter(wapid=q_wapid).distinct()
   
    if object_list :
        model_name = '游戏'
	object = object_list[0]
        return HttpResponse(loader.render_to_string('wap/search_detail.wml',{'model_name':model_name,'object':object}),"text/vnd.wap.wml")
        
    object_list = Book.objects.filter(wapid=q_wapid).distinct()
   
    if object_list :
        model_name = '小说'
	object = object_list[0]
        return HttpResponse(loader.render_to_string('wap/search_detail.wml',{'model_name':model_name,'object':object}),"text/vnd.wap.wml")
    
    return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")

#搜索soft
def search_soft(request,page='1'):
    q_keyword = request.GET.get('keyword', '')
    zhonglei = 'soft'
    if q_keyword == '':
        return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    
    
    object_list = Soft.objects.filter(name__icontains=q_keyword).order_by('-datetime').distinct()
   
    if object_list :
        model_name = '软件'
	count = object_list.count()
	page_count = 0
	page_int = int(page)
	yushu = count % 10
	if yushu == 0:
	    page_count = count / 10
	else:
	    page_count = count / 10 + 1
    
	if count == 0:
            page_count = 1
	if page_int*10 <= count:
            object_list = object_list[(page_int-1)*10:page_int*10]
	else:
            object_list = object_list[(page_int-1)*10:count]
    
	cur_page = page_int
	has_prev = False
	has_next = False
	prev_page = 0
	next_page = 0
	if cur_page == 1:
            has_prev = False
	else:
            has_prev = True
            prev_page = cur_page-1
        if cur_page == page_count:
            has_next = False
        else:
            has_next = True
            next_page = cur_page+1
        count_list = range(1,page_count+1)
        return HttpResponse(loader.render_to_string('wap/search_list.wml',{'zhonglei':zhonglei,'page':page_int,'count':count,'page_count':page_count,'has_prev':has_prev,'has_next':has_next,'prev_page':prev_page,'next_page':next_page,'keyword':q_keyword,'model_name':model_name,'object_list':object_list}),"text/vnd.wap.wml")
    
        
    return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")

#搜索game
def search_game(request,page='1'):
    q_keyword = request.GET.get('keyword', '')
    zhonglei = 'game'
    if q_keyword == '':
        return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    
    object_list = Game.objects.filter(name__icontains=q_keyword).order_by('-datetime').distinct()
    
    if object_list :
        model_name = '游戏'
	count = object_list.count()
	page_count = 0
	page_int = int(page)
	yushu = count % 10
	if yushu == 0:
	    page_count = count / 10
	else:
	    page_count = count / 10 + 1
    
	if count == 0:
            page_count = 1
	if page_int*10 <= count:
            object_list = object_list[(page_int-1)*10:page_int*10]
	else:
            object_list = object_list[(page_int-1)*10:count]
    
	cur_page = page_int
	has_prev = False
	has_next = False
	prev_page = 0
	next_page = 0
	if cur_page == 1:
            has_prev = False
	else:
            has_prev = True
            prev_page = cur_page-1
        if cur_page == page_count:
            has_next = False
        else:
            has_next = True
            next_page = cur_page+1
        count_list = range(1,page_count+1)
        return HttpResponse(loader.render_to_string('wap/search_list.wml',{'zhonglei':zhonglei,'page':page_int,'count':count,'page_count':page_count,'has_prev':has_prev,'has_next':has_next,'prev_page':prev_page,'next_page':next_page,'keyword':q_keyword,'model_name':model_name,'object_list':object_list}),"text/vnd.wap.wml")
        
    
    
    return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")

#搜索
def search_phone_game(request,page='1'):
    q_keyword = request.GET.get('keyword', '')
    q_phone = request.GET.get('phone', '')
    if q_keyword == '':
        return HttpResponse(loader.render_to_string('wap/no_phone_detail.wml',{}),"text/vnd.wap.wml")
    object_list = Game.objects.filter(name__icontains=q_keyword).filter(os__icontains=q_phone).order_by('-datetime').distinct()
    if object_list :
        model_name = '游戏'
	count = object_list.count()
	page_count = 0
	page_int = int(page)
	yushu = count % 10
	if yushu == 0:
	    page_count = count / 10
	else:
	    page_count = count / 10 + 1
    
	if count == 0:
            page_count = 1
	if page_int*10 <= count:
            object_list = object_list[(page_int-1)*10:page_int*10]
	else:
            object_list = object_list[(page_int-1)*10:count]
    
	cur_page = page_int
	has_prev = False
	has_next = False
	prev_page = 0
	next_page = 0
	if cur_page == 1:
            has_prev = False
	else:
            has_prev = True
            prev_page = cur_page-1
        if cur_page == page_count:
            has_next = False
        else:
            has_next = True
            next_page = cur_page+1
        count_list = range(1,page_count+1)
        return HttpResponse(loader.render_to_string('wap/phone_game_search.wml',{'phone':q_phone,'keyword':q_keyword,'page':page_int,'count':count,'page_count':page_count,'has_prev':has_prev,'has_next':has_next,'prev_page':prev_page,'next_page':next_page,'keyword':q_keyword,'model_name':model_name,'object_list':object_list}),"text/vnd.wap.wml")
        
    
    
    return HttpResponse(loader.render_to_string('wap/no_phone_detail.wml',{}),"text/vnd.wap.wml")


#搜索
def search_xiaoshuo(request,page='1'):
    q_keyword = request.GET.get('keyword', '')
    zhonglei = 'xiaoshuo'
    if q_keyword == '':
        return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    
    
    object_list = Book.objects.filter(name__icontains=q_keyword).order_by('-datetime').distinct()
   
    if object_list :
        model_name = '小说'
	
        count = object_list.count()
	page_count = 0
	page_int = int(page)
	yushu = count % 10
	if yushu == 0:
	    page_count = count / 10
	else:
	    page_count = count / 10 + 1
    
	if count == 0:
            page_count = 1
	if page_int*10 <= count:
            object_list = object_list[(page_int-1)*10:page_int*10]
	else:
            object_list = object_list[(page_int-1)*10:count]
    
	cur_page = page_int
	has_prev = False
	has_next = False
	prev_page = 0
	next_page = 0
	if cur_page == 1:
            has_prev = False
	else:
            has_prev = True
            prev_page = cur_page-1
        if cur_page == page_count:
            has_next = False
        else:
            has_next = True
            next_page = cur_page+1
        count_list = range(1,page_count+1)
        return HttpResponse(loader.render_to_string('wap/search_list.wml',{'zhonglei':zhonglei,'page':page_int,'count':count,'page_count':page_count,'has_prev':has_prev,'has_next':has_next,'prev_page':prev_page,'next_page':next_page,'keyword':q_keyword,'model_name':model_name,'object_list':object_list}),"text/vnd.wap.wml")
    
    return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")

def soft_top50(request,page='1'):
    #软件TOP50
    model_name = '软件'
    model_name_english = 'soft'
    soft_top = Soft.objects.order_by('-downcount')[:50]
    count = soft_top.count()
    page_count = 0
    page_int = int(page)
    yushu = count % 10
    if yushu == 0:
      page_count = count / 10
    else:
      page_count = count / 10 + 1
    
    if count == 0:
        page_count = 1
    if page_int*10 <= count:
        soft_top = soft_top[(page_int-1)*10:page_int*10]
    else:
        soft_top = soft_top[(page_int-1)*10:count]
    
    return HttpResponse(loader.render_to_string('wap/top50.wml',{'object_top':soft_top,'model_name':model_name,'page':page_int,'model_name_english':model_name_english}),"text/vnd.wap.wml")
def game_top50(request,page='1'):
    #游戏TOP50
    model_name = '游戏'
    model_name_english = 'game'
    game_top = Game.objects.order_by('-downcount')[:50]
    count = game_top.count()
    page_count = 0
    page_int = int(page)
    yushu = count % 10
    if yushu == 0:
      page_count = count / 10
    else:
      page_count = count / 10 + 1
    
    if count == 0:
        page_count = 1
    if page_int*10 <= count:
        game_top = game_top[(page_int-1)*10:page_int*10]
    else:
        game_top = soft_top[(page_int-1)*10:count]
    
    return HttpResponse(loader.render_to_string('wap/top50.wml',{'object_top':game_top,'model_name':model_name,'page':page_int,'model_name_english':model_name_english}),"text/vnd.wap.wml")
def book_top50(request,page='1'):
    #小说TOP50
    model_name = '小说'
    model_name_english = 'book'

    book_top = Book.objects.order_by('-downcount')[:50]
    count = book_top.count()
    page_count = 0
    page_int = int(page)
    yushu = count % 10
    if yushu == 0:
      page_count = count / 10
    else:
      page_count = count / 10 + 1
    
    if count == 0:
        page_count = 1
    if page_int*10 <= count:
        book_top = book_top[(page_int-1)*10:page_int*10]
    else:
        book_top = book_top[(page_int-1)*10:count]
    
    
    return HttpResponse(loader.render_to_string('wap/top50.wml',{'object_top':book_top,'model_name':model_name,'page':page_int,'model_name_english':model_name_english}),"text/vnd.wap.wml")


#Wap快车道
def down_detail(request,id,model,template):
    q_phone = request.GET.get('phone', 'N70')
    
    if q_phone == '':
        return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    object = get_object_or_404(model, pk=id)
        
    return HttpResponse(loader.render_to_string(template,{'phone':q_phone,'object':object}),"text/vnd.wap.wml")


#分类页面
def show_cate(request,name):
    
    model_name = ''
    cate_list = []
    if name == 'soft':
        model_name = '软件'
	cate_list = SoftCate.objects.all()
	return HttpResponse(loader.render_to_string('wap/cate.wml',{'cate_list':cate_list,'model_name':model_name}),"text/vnd.wap.wml")
    if name == 'game' :
        model_name = '游戏'
	cate_list = GameCate.objects.all()
	return HttpResponse(loader.render_to_string('wap/cate.wml',{'cate_list':cate_list,'model_name':model_name}),"text/vnd.wap.wml")
    if name == 'book':
        model_name = '小说'
	cate_list = BookCate.objects.all()
	return HttpResponse(loader.render_to_string('wap/cate.wml',{'cate_list':cate_list,'model_name':model_name}),"text/vnd.wap.wml")
    else :
        return HttpResponse(loader.render_to_string('wap/error.wml',{}),"text/vnd.wap.wml")

#软件频道首页
def show_soft_index(request):
    
    cate_list = SoftCate.objects.all()
    
        
    return HttpResponse(loader.render_to_string('wap/soft_index.wml',{'cate_list':cate_list}),"text/vnd.wap.wml")


#列表页面
def down_list(request,id,cate,model,template):
    
    page = request.GET.get('page', '1')
    sort_by = request.GET.get('sort_by', 'time')
    cate_object = get_object_or_404(cate, pk=id)
    if sort_by == 'time':
        
        object_list = model.objects.filter(cate=cate_object).order_by('-datetime')
    elif sort_by == 'hot':
        object_list = model.objects.filter(cate=cate_object).order_by('-downcount')
    else:
        object_list = model.objects.filter(cate=cate_object).order_by('-datetime')
    
    #分页
    count = object_list.count()
    page_count = 0
    page_int = int(page)
    yushu = count % 10
    if yushu == 0:
      page_count = count / 10
    else:
      page_count = count / 10 + 1
    
    if count == 0:
        page_count = 1
    if page_int*10 <= count:
        object_list = object_list[(page_int-1)*10:page_int*10]
    else:
        object_list = object_list[(page_int-1)*10:count]
    
    cur_page = page_int
    has_prev = False
    has_next = False
    prev_page = 0
    next_page = 0
    if cur_page == 1:
       has_prev = False
    else:
       has_prev = True
       prev_page = cur_page-1
    if cur_page == page_count:
       has_next = False
    else:
       has_next = True
       next_page = cur_page+1
    count_list = range(1,page_count+1)
    
    return HttpResponse(loader.render_to_string(template,{'sort_by':sort_by,'page':page_int,'count':count,'page_count':page_count,'has_prev':has_prev,'has_next':has_next,'prev_page':prev_page,'next_page':next_page,'object_list':object_list,'cate_object':cate_object}),"text/vnd.wap.wml")


#游戏频道首页
def show_game_index(request):
    
    nokia_list = Phone.objects.filter(factory='诺基亚').order_by('-count')[0:16]
    nokia_list1 = nokia_list[0:4]
    nokia_list2 = nokia_list[4:8]
    nokia_list3 = nokia_list[8:12]
    nokia_list4 = nokia_list[12:16]
    moto_list = Phone.objects.filter(factory='摩托罗拉').order_by('-count')[0:12]
    moto_list1 = moto_list[0:4]
    moto_list2 = moto_list[4:8]
    moto_list3 = moto_list[8:12]
    sony_list = Phone.objects.filter(factory='索爱').order_by('-count')[0:12]
    sony_list1 = sony_list[0:4]
    sony_list2 = sony_list[4:8]
    sony_list3 = sony_list[8:12]
    return HttpResponse(loader.render_to_string('wap/game_index.wml',{'nokia_list1':nokia_list1,'nokia_list2':nokia_list2,'nokia_list3':nokia_list3,'nokia_list4':nokia_list4,'moto_list1':moto_list1,'moto_list2':moto_list2,'moto_list3':moto_list3,'sony_list1':sony_list1,'sony_list2':sony_list2,'sony_list3':sony_list3}),"text/vnd.wap.wml")



#小说频道首页
def show_book_index(request):
    
    #cate_list = SoftCate.objects.all()
    name = 'book'
    if name == 'book':
        model_name = '小说'
	cate_list = BookCate.objects.all()
	return HttpResponse(loader.render_to_string('wap/book_index.wml',{'cate_list':cate_list,'model_name':model_name}),"text/vnd.wap.wml")
    else :
        return HttpResponse(loader.render_to_string('wap/error.wml',{}),"text/vnd.wap.wml")
        
    #return HttpResponse(loader.render_to_string('wap/book_index.wml',{'cate_list':cate_list}),"text/vnd.wap.wml")

#游戏机型首页
def show_phone_index(request):
    q_phone = request.GET.get('phone', '')
    
    if q_phone == '':
        return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    
    phones = Phone.objects.filter(phone_name=q_phone).distinct()

    if phones:
        phone = phones[0]
	count = int(phone.count)
	phone.count = str(count+1)
	phone.save()
    games = Game.objects.filter(os__icontains=q_phone).order_by('-datetime').distinct()
    count = games.count()
    news = games[0:5]
    
    tuijis = Game.objects.filter(os__icontains=q_phone).order_by('?').distinct()[0:5]
    #cate_list = GameCate.objects.all()
    
        
    return HttpResponse(loader.render_to_string('wap/phone.wml',{'count':count,'phone':q_phone,'tuijis':tuijis,'news':news}),"text/vnd.wap.wml")

#游戏OS列表
def show_os_list(request):
    q_brand = request.GET.get('brand', '')
    page = request.GET.get('page', '1')
    if q_brand == '':
        return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    brand_name = ''
    if q_brand == 'Nokia':
        brand_name = 'Nokia'
        object_list = Phone.objects.filter(factory='诺基亚').order_by('-count')
        #return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    if q_brand == 'Motorola':
        brand_name = 'Motorola'
        object_list = Phone.objects.filter(factory='摩托罗拉').order_by('-count')
        #return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    if q_brand == 'sonyericsson':
        brand_name = 'sonyericsson'
        object_list = Phone.objects.filter(factory='索爱').order_by('-count')
        #return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    
    
    count = object_list.count()
    page_count = 0
    page_int = int(page)
    yushu = count % 40
    if yushu == 0:
      page_count = count / 40
    else:
      page_count = count / 40 + 1
    
    if count == 0:
        page_count = 1
    if page_int*40 <= count:
        object_list = object_list[(page_int-1)*40:page_int*40]
    else:
        object_list = object_list[(page_int-1)*40:count]
    object_list1 = []
    object_list2 = []
    object_list3 = []
    object_list4 = []
    object_list5 = []
    object_list6 = []
    object_list7 = []
    object_list8 = []
    object_list9 = []
    object_list10 = []
    if object_list.count() < 4:
        object_list1 = object_list[0:object_list.count()]
    elif object_list.count() < 8:
        object_list1 = object_list[0:4]
	object_list2 = object_list[4:object_list.count()]
    elif object_list.count() < 12:
        object_list1 = object_list[0:4]
	object_list2 = object_list[4:8]
	object_list3 = object_list[8:object_list.count()]
    elif object_list.count() < 16:
        object_list1 = object_list[0:4]
	object_list2 = object_list[4:8]
	object_list3 = object_list[8:12]
	object_list4 = object_list[12:object_list.count()]
    elif object_list.count() < 20:
        object_list1 = object_list[0:4]
	object_list2 = object_list[4:8]
	object_list3 = object_list[8:12]
	object_list4 = object_list[12:16]
        object_list5 = object_list[16:object_list.count()]
    elif object_list.count() < 24:
        object_list1 = object_list[0:4]
	object_list2 = object_list[4:8]
	object_list3 = object_list[8:12]
	object_list4 = object_list[12:16]
        object_list5 = object_list[16:20]
        object_list6 = object_list[20:object_list.count()]
    elif object_list.count() < 28:
        object_list1 = object_list[0:4]
	object_list2 = object_list[4:8]
	object_list3 = object_list[8:12]
	object_list4 = object_list[12:16]
        object_list5 = object_list[16:20]
        object_list6 = object_list[20:24]
        object_list7 = object_list[24:object_list.count()]
    elif object_list.count() < 32:
        object_list1 = object_list[0:4]
	object_list2 = object_list[4:8]
	object_list3 = object_list[8:12]
	object_list4 = object_list[12:16]
        object_list5 = object_list[16:20]
        object_list6 = object_list[20:24]
        object_list7 = object_list[24:28]
        object_list8 = object_list[28:object_list.count()]
    elif object_list.count() < 36:
        object_list1 = object_list[0:4]
	object_list2 = object_list[4:8]
	object_list3 = object_list[8:12]
	object_list4 = object_list[12:16]
        object_list5 = object_list[16:20]
        object_list6 = object_list[20:24]
        object_list7 = object_list[24:28]
        object_list8 = object_list[28:32]
        object_list9 = object_list[32:object_list.count()]
    else:
        object_list1 = object_list[0:4]
	object_list2 = object_list[4:8]
	object_list3 = object_list[8:12]
	object_list4 = object_list[12:16]
        object_list5 = object_list[16:20]
        object_list6 = object_list[20:24]
        object_list7 = object_list[24:28]
        object_list8 = object_list[28:32]
        object_list9 = object_list[32:36]
        object_list10 = object_list[36:object_list.count()]
    cur_page = page_int
    has_prev = False
    has_next = False
    prev_page = 0
    next_page = 0
    if cur_page == 1:
       has_prev = False
    else:
       has_prev = True
       prev_page = cur_page-1
    if cur_page == page_count:
       has_next = False
    else:
       has_next = True
       next_page = cur_page+1
    
        
    return HttpResponse(loader.render_to_string('wap/os.wml',{'object_list1':object_list1,'object_list2':object_list2,'object_list3':object_list3,'object_list4':object_list4,'object_list5':object_list5,'object_list6':object_list6,'object_list7':object_list7,'object_list8':object_list8,'object_list9':object_list9,'object_list10':object_list10,'brand_name':brand_name,'page':page_int,'count':count,'page_count':page_count,'has_prev':has_prev,'has_next':has_next,'prev_page':prev_page,'next_page':next_page,'object_list':object_list}),"text/vnd.wap.wml")

def show_os_help(request):

    return HttpResponse(loader.render_to_string('wap/os_help.wml',{}),"text/vnd.wap.wml")

#游戏机型new首页
def show_game_new(request):
    q_phone = request.GET.get('phone', '')
    
    if q_phone == '':
        return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    
    
    object_list = Game.objects.filter(os__icontains=q_phone).distinct()
    
    #cate_list = GameCate.objects.all()
    
        
    return HttpResponse(loader.render_to_string('wap/phone.wml',{'phone':q_phone,'cate_list':cate_list}),"text/vnd.wap.wml")

#游戏机型top首页
def show_game_top(request,page='1'):
    q_phone = request.GET.get('phone', '')
        
    if q_phone == '':
        return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
        
    object_list = Game.objects.filter(os__icontains=q_phone).order_by('-downcount').distinct()
    object_list = object_list[0:100]
    count = object_list.count()
    page_count = 0
    page_int = int(page)
    yushu = count % 10
    if yushu == 0:
       page_count = count / 10
    else:
       page_count = count / 10 + 1
    
    if count == 0:
       page_count = 1
    if page_int*10 <= count:
       object_list = object_list[(page_int-1)*10:page_int*10]
    else:
       object_list = object_list[(page_int-1)*10:count]
    
    cur_page = page_int
    has_prev = False
    has_next = False
    prev_page = 0
    next_page = 0
    if cur_page == 1:
       has_prev = False
    else:
       has_prev = True
       prev_page = cur_page-1
    if cur_page == page_count:
       has_next = False
    else:
       has_next = True
       next_page = cur_page+1

    flag = '热门'
    return HttpResponse(loader.render_to_string('wap/top_game_list.wml',{'flag':flag,'page':page_int,'count':count,'page_count':page_count,'has_prev':has_prev,'has_next':has_next,'prev_page':prev_page,'next_page':next_page,'phone':q_phone,'object_list':object_list}),"text/vnd.wap.wml")



    

#游戏推荐
def show_game_recomm(request,page='1'):
    q_phone = request.GET.get('phone', '')
        
    if q_phone == '':
        return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
        
    object_list = Game.objects.filter(os__icontains=q_phone).order_by('?').distinct()
    object_list = object_list[0:100]
    count = object_list.count()
    page_count = 0
    page_int = int(page)
    yushu = count % 10
    if yushu == 0:
       page_count = count / 10
    else:
       page_count = count / 10 + 1
    
    if count == 0:
       page_count = 1
    if page_int*10 <= count:
       object_list = object_list[(page_int-1)*10:page_int*10]
    else:
       object_list = object_list[(page_int-1)*10:count]
    
    cur_page = page_int
    has_prev = False
    has_next = False
    prev_page = 0
    next_page = 0
    if cur_page == 1:
       has_prev = False
    else:
       has_prev = True
       prev_page = cur_page-1
    if cur_page == page_count:
       has_next = False
    else:
       has_next = True
       next_page = cur_page+1

    flag = '推荐'
    return HttpResponse(loader.render_to_string('wap/commend_game_list.wml',{'flag':flag,'page':page_int,'count':count,'page_count':page_count,'has_prev':has_prev,'has_next':has_next,'prev_page':prev_page,'next_page':next_page,'phone':q_phone,'object_list':object_list}),"text/vnd.wap.wml")


#游戏机型cate首页
def show_game_cate(request):
    q_phone = request.GET.get('phone', '')
    q_cate = request.GET.get('cate', '')
    
    page = request.GET.get('page', '1')
    sort_by = request.GET.get('sort_by', 'time')
    
    
    if q_phone == '':
        return HttpResponse(loader.render_to_string('wap/no_detail.wml',{}),"text/vnd.wap.wml")
    if q_cate == '':
        q_cate = '0'

    q_cate = int(q_cate)
    
    
    if q_cate > 0 and q_cate <= 9:
        if sort_by == 'time':
        
            object_list = Game.objects.filter(os__icontains=q_phone).filter(cate=q_cate).order_by('-datetime').distinct()
        elif sort_by == 'hot':
            object_list = Game.objects.filter(os__icontains=q_phone).filter(cate=q_cate).order_by('-downcount')
        else:
            object_list = Game.objects.filter(os__icontains=q_phone).filter(cate=q_cate).order_by('-datetime').distinct()
        #object_list = Game.objects.filter(os__icontains=q_phone).filter(cate=q_cate).order_by('-datetime').distinct()
        cate_object = get_object_or_404(GameCate, pk=q_cate)
        count = object_list.count()
        page_count = 0
        page_int = int(page)
        yushu = count % 10
        if yushu == 0:
            page_count = count / 10
        else:
            page_count = count / 10 + 1
    
        if count == 0:
            page_count = 1
        if page_int*10 <= count:
            object_list = object_list[(page_int-1)*10:page_int*10]
        else:
            object_list = object_list[(page_int-1)*10:count]
    
        cur_page = page_int
        has_prev = False
        has_next = False
        prev_page = 0
        next_page = 0
        if cur_page == 1:
           has_prev = False
        else:
           has_prev = True
           prev_page = cur_page-1
        if cur_page == page_count:
           has_next = False
        else:
           has_next = True
           next_page = cur_page+1
	
        flag = '最新'
	return HttpResponse(loader.render_to_string('wap/phone_game_list.wml',{'sort_by':sort_by,'flag':flag,'page':page_int,'count':count,'page_count':page_count,'has_prev':has_prev,'has_next':has_next,'prev_page':prev_page,'next_page':next_page,'cate_object':cate_object,'phone':q_phone,'object_list':object_list}),"text/vnd.wap.wml")

    else:
        object_list = Game.objects.filter(os__icontains=q_phone).order_by('-datetime').distinct()
    
        count = object_list.count()
        page_count = 0
        page_int = int(page)
        yushu = count % 10
        if yushu == 0:
           page_count = count / 10
        else:
           page_count = count / 10 + 1
    
        if count == 0:
           page_count = 1
        if page_int*10 <= count:
           object_list = object_list[(page_int-1)*10:page_int*10]
        else:
           object_list = object_list[(page_int-1)*10:count]
    
        cur_page = page_int
        has_prev = False
        has_next = False
        prev_page = 0
        next_page = 0
        if cur_page == 1:
           has_prev = False
        else:
           has_prev = True
           prev_page = cur_page-1
        if cur_page == page_count:
           has_next = False
        else:
           has_next = True
           next_page = cur_page+1

        flag = '最新'
        return HttpResponse(loader.render_to_string('wap/phone_game_list.wml',{'sort_by':sort_by,'flag':flag,'page':page_int,'count':count,'page_count':page_count,'has_prev':has_prev,'has_next':has_next,'prev_page':prev_page,'next_page':next_page,'phone':q_phone,'object_list':object_list}),"text/vnd.wap.wml")


#社区forum首页
def show_forum_index(request):
    
    return HttpResponse(loader.render_to_string('wap/forum_index.wml',{}),"text/vnd.wap.wml")


def show_book_pic(request,id):
    
    name = '小说'
    ename = 'book'
    object = get_object_or_404(Book, pk=id)
    object.pic = object.pic.replace('\\','/',9999)
    return HttpResponse(loader.render_to_string('wap/show_pic.wml',{'ename':ename,'name':name,'object':object}),"text/vnd.wap.wml")

def show_game_pic(request,id):
    
    name = '游戏'
    ename = 'game'
    object = get_object_or_404(Game, pk=id)
    object.pic = object.pic.replace('\\','/',9999)
    return HttpResponse(loader.render_to_string('wap/show_pic.wml',{'ename':ename,'name':name,'object':object}),"text/vnd.wap.wml")
 
def show_soft_pic(request,id):
    
    name = '软件'
    ename = 'soft'
    object = get_object_or_404(Soft, pk=id)
    object.pic = object.pic.replace('\\','/',9999)
    return HttpResponse(loader.render_to_string('wap/show_pic.wml',{'ename':ename,'name':name,'object':object}),"text/vnd.wap.wml")

def show_book_info(request,id):

    name = '小说'
    ename = 'book'
    object = get_object_or_404(Book, pk=id)
    object.intro = object.intro.replace('<br>','<br/>',9999)
    object.intro = object.intro.replace('</br>','<br/>',9999)
    return HttpResponse(loader.render_to_string('wap/show_info.wml',{'ename':ename,'name':name,'object':object}),"text/vnd.wap.wml")

def show_game_info(request,id):

    name = '游戏'
    ename = 'game'
    object = get_object_or_404(Game, pk=id)
    object.intro = object.intro.replace('<br>','<br/>',9999)
    object.intro = object.intro.replace('</br>','<br/>',9999)
    return HttpResponse(loader.render_to_string('wap/show_info.wml',{'ename':ename,'name':name,'object':object}),"text/vnd.wap.wml")

def show_soft_info(request,id):

    name = '软件'
    ename = 'soft'
    object = get_object_or_404(Soft, pk=id)
    object.intro = object.intro.replace('<br>','<br/>',9999)
    object.intro = object.intro.replace('</br>','<br/>',9999)

    return HttpResponse(loader.render_to_string('wap/show_info.wml',{'ename':ename,'name':name,'object':object}),"text/vnd.wap.wml")

#显示友情链接首页
def show_friendslink_index(request):
    
    page = request.GET.get('page', '1')
        
    object_list = FriendLink.objects.order_by('count')

    #分页
    count = object_list.count()
    page_count = 0
    page_int = int(page)
    yushu = count % 10
    if yushu == 0:
      page_count = count / 10
    else:
      page_count = count / 10 + 1
    
    if count == 0:
        page_count = 1
    if page_int*10 <= count:
        object_list = object_list[(page_int-1)*10:page_int*10]
    else:
        object_list = object_list[(page_int-1)*10:count]
    
    cur_page = page_int
    has_prev = False
    has_next = False
    prev_page = 0
    next_page = 0
    if cur_page == 1:
       has_prev = False
    else:
       has_prev = True
       prev_page = cur_page-1
    if cur_page == page_count:
       has_next = False
    else:
       has_next = True
       next_page = cur_page+1
    count_list = range(1,page_count+1)

    return HttpResponse(loader.render_to_string('wap/friendslink.wml',{'page':page_int,'count':count,'page_count':page_count,'has_prev':has_prev,'has_next':has_next,'prev_page':prev_page,'next_page':next_page,'object_list':object_list}),"text/vnd.wap.wml")


#显示提交友情链接页
def show_friendslink_submit(request):
    
    return HttpResponse(loader.render_to_string('wap/coop.wml',{}),"text/vnd.wap.wml")

#处理提交友情链
def do_friendslink_submit(request):
    netname = request.POST.get('netname', '')
    netaddr = request.POST.get('netaddr', '')
    q_email = request.POST.get('email', '')
    qq = request.POST.get('qqnumber', '')

    if netname == '' or netaddr == '' or q_email == '' or qq == '':
        error = '对不起，所有项目都需要正确输入，请重新输入！'
        return HttpResponse(loader.render_to_string('wap/coop_error.wml',{'error':error}),"text/vnd.wap.wml")

    if q_email.find('@') == -1:
        error = '对不起，您输入的Email地址有误,请重新输入！'
        return HttpResponse(loader.render_to_string('wap/coop_error.wml',{'error':error}),"text/vnd.wap.wml")

    try:
       int(qq)
    except:
        error = '对不起，您输入的QQ号码有误,请重新输入！'
        return HttpResponse(loader.render_to_string('wap/coop_error.wml',{'error':error}),"text/vnd.wap.wml")

    netaddr = netaddr.replace('http://','')

    fl = FriendsLinks(name=netname,link=netaddr,count=0,email=q_email,qqnumber=qq,isCheck=False)

    fl.save()

    return HttpResponse(loader.render_to_string('wap/coop_success.wml',{}),"text/vnd.wap.wml")